package com.lvbaoins.Harmony.system.common.factory;

import com.lvbaoins.Harmony.system.common.Strategy;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class ActionFactory {

    private static Map<String, Strategy> factory = new HashMap<>();

    private static Strategy empty = new Strategy() {
        @Override
        public void exec(String orderNo, HttpServletResponse response) {

        }

        @Override
        public void downLoadBatch(Integer[] idArr, HttpServletRequest request, HttpServletResponse response) throws Exception {

        }

        @Override
        public void afterPropertiesSet() throws Exception {

        }
    };

    /**
     * 注册
     * @param key
     * @param strategy
     */
    public static void reg(String key,Strategy strategy){
        factory.put(key,strategy);
    }

    /**
     * 获取
     * @param key
     * @return
     */
    public static Strategy get(String key){
        Strategy strategy = factory.get(key);
        if(strategy == null){
            log.error("获取下载变量失败:产品编码:{}",key);
        }
        return strategy == null ? empty : strategy;
    }
}
